<template>
  <div class="setting-password">
    <Header to="/personal/setting" title="密码设置"></Header>
    <div class="setting-password-change">
      <el-form :model="passwordFrom" status-icon :rules="rules" ref="passwordFrom">
        <p>旧密码</p>
        <el-form-item prop="oldPass">
          <el-input type="password" v-model="passwordFrom.oldPass" autocomplete="off"></el-input>
        </el-form-item>
        <p>新密码</p>
        <el-form-item prop="newPass">
          <el-input type="password" v-model="passwordFrom.newPass" autocomplete="off"></el-input>
        </el-form-item>
        <p>确认密码</p>
        <el-form-item prop="verification">
          <el-input
            class="auth_input"
            type="password"
            v-model="passwordFrom.checkPass"
          />
        </el-form-item>
        <el-form-item class="password-change-sumbit">
          <span>
            <router-link to="/personal/login/pass-forget">忘记密码 ?</router-link>
          </span>
            <el-button type="primary" @click="makeSure()">确认 提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { garbageMixin } from '../../utils/mixin'
import Header from '../common/Header'
import axios from 'axios'
import qs from 'qs'
import { setLocalStorage, getLocalStorage } from '../../utils/localStorage'

export default {
  mixins: [garbageMixin],
  components: {
    Header
  },
  data () {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        if (this.passwordFrom.checkPass !== '') {
          this.$refs.passwordFrom.validateField('checkPass')
        }
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.passwordFrom.newPass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    const validatorPass3 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入旧密码'))
      } else {
        callback()
      }
    }

    return {
      sendAuthCode: true,
      passwordFrom: {
        oldPass: '',
        newPass: '',
        checkPass: ''
      },
      rules: {
        oldPass: [
          { required: true, validator: validatorPass3, trigger: 'blur' }
        ],
        newPass: [{ required: true, validator: validatePass, trigger: 'blur' }],
        checkPass: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    makeSure () {
      // 数据提交
      let oldPassword = sessionStorage.getItem('password')
      console.log(this.passwordFrom.oldPass, oldPassword, 'aa')
      if (this.passwordFrom.oldPass !== oldPassword) {
        this.$message({
          message: '输入的旧密码不正确',
          type: 'warning'
        })
      } else {
        let prefix = '/user/updatePwd'
        let data = qs.stringify({
          password: this.passwordFrom.checkPass
        })
        console.log(data, 'data')
        let sessionId = getLocalStorage('sessionId')
        axios.put(prefix, data, {headers: {'sessionId': sessionId}})
          .then((response) => {
            console.log(response, 'response')
            let success = response.data.code
            console.log(response, 'uuu', response.status, response.data, success)
            if (success === 1) {
              this.$message({
                message: '密码修改成功，请重新登录',
                type: 'success'
              })
              this.setUserPassword(this.passwordFrom.checkPass)
              setLocalStorage('userPassword', this.passwordFrom.checkPass)
              sessionStorage.password = this.passwordFrom.checkPass
              this.$router.push('/personal/login')
            } else {
              this.$message({
                message: '密码修改失败',
                type: 'warning'
              })
            }
          })
          .catch((error) => {
            console.log(error, 'kk')
          })
      }
    }

  }
}
</script>

<style lang="scss" scoped>
@import "../../assets/styles/global";
.setting-password {
  width: 100%;
  .setting-password-change {
    width: 95%;
    border-radius: 6px;
    padding: 100px 12px;
    box-sizing: border-box;
    margin: 0 auto;
    .el-form {
      position: relative;
      p {
        padding-left: 12px;
        height: 22px;
        line-height: 22px;
      }
      .el-form-item {
        margin-bottom: 16px;
        span {
          position: absolute;
          top: -32px;
          right: 12px;
          a {
            color: #555;
          }
        }
      }
      .password-change-sumbit {
        text-align: center;
        padding-top: 12px;
      }
    }
  }
}
</style>
